package adapter;

import java.util.List;

import javax.swing.JComponent;
import javax.swing.JTextArea;

import protocal.DataPackage;
import protocal.DataPackage.MessageType;
import util.MessageUtil;

public class MessageAdapter extends Adapter {
	private JTextArea jTextArea;
	private List<DataPackage> dataMessages;
	
	


	public MessageAdapter(JComponent jTextArea,  List<DataPackage> dataMessages) {
		super();
		this.jTextArea = (JTextArea) jTextArea;
		this.dataMessages = dataMessages;
	}




	@Override
	public void adapter() {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(" ALL\n");
		for (int i = 0; i < dataMessages.size(); i++) {
			if (i<dataMessages.size()-1) {
				stringBuilder.append(" "+dataMessages.get(i).toString()+"\n");
			}else{
				stringBuilder.append(" "+dataMessages.get(i).toString());
			}
			
		}
		jTextArea.setText(stringBuilder.toString());

	}




	@Override
	public void adapter(Object object) {
		DataPackage dataMessage = (DataPackage) object;
		dataMessages.add(dataMessage);
		if (dataMessage.getMessageType()==MessageType.MESSAGE) {
			jTextArea.append(MessageUtil.buildMessage(dataMessage));
		}
		if (dataMessage.getMessageType()==MessageType.SHAKE) {
			jTextArea.append(MessageUtil.buildShake(dataMessage));
		}
		
		
	}

}
